package org.example;

import ch.qos.logback.core.testUtil.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.shaded.commons.lang3.RandomUtils;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.springframework.stereotype.Service;

import java.util.Random;


@Service
@Slf4j
@RocketMQMessageListener(consumerGroup = "event-mq-group", topic = "test-topic",
        consumeMode = ConsumeMode.CONCURRENTLY, messageModel = MessageModel.BROADCASTING)
public class MessageConsumer implements RocketMQListener<String>, RocketMQPushConsumerLifecycleListener {

    @Override
    public void onMessage(String message) {
        System.out.println("消费消息:" + message);
    }

    @Override
    public void prepareStart(DefaultMQPushConsumer consumer) {
        //设置当前实例的名称
        consumer.setInstanceName("consumer" + RandomStringUtils.random(6));
    }
}
